package com.ruoyi.wvp.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wvp.domain.CardReader;
import com.ruoyi.wvp.domain.CommunityEventCount;
import com.ruoyi.wvp.service.ICommunityEventCountService;
import com.ruoyi.wvp.utils.PermissionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wvp.mapper.SurveillanceEventMapper;
import com.ruoyi.wvp.domain.SurveillanceEvent;
import com.ruoyi.wvp.service.ISurveillanceEventService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.enums.DeletedEnum;
import com.ruoyi.common.utils.StringUtils;

import java.util.Objects;

/**
 * ai摄像头事件Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-29
 */
@Service
@Slf4j
public class SurveillanceEventServiceImpl extends ServiceImpl<SurveillanceEventMapper, SurveillanceEvent> implements ISurveillanceEventService {
    @Autowired
    private SurveillanceEventMapper surveillanceEventMapper;
    @Autowired
    private ICommunityEventCountService commintyEventCountService;

    /**
     * 查询ai摄像头事件
     *
     * @param id ai摄像头事件主键
     * @return ai摄像头事件
     */
    @Override
    public SurveillanceEvent selectSurveillanceEventById(Long id) {
        return surveillanceEventMapper.selectSurveillanceEventById(id);
    }

    /**
     * 查询ai摄像头事件列表
     *
     * @param surveillanceEvent ai摄像头事件
     * @return ai摄像头事件
     */
    @Override
    public List<SurveillanceEvent> selectSurveillanceEventList(SurveillanceEvent surveillanceEvent) {
        return surveillanceEventMapper.selectSurveillanceEventList(surveillanceEvent);
    }

    /**
     * 新增ai摄像头事件
     *
     * @param surveillanceEvent ai摄像头事件
     * @return 结果
     */
    @Override
    public int insertSurveillanceEvent(SurveillanceEvent surveillanceEvent) {
        surveillanceEvent.setCreateTime(DateUtils.getNowDate());
        return surveillanceEventMapper.insertSurveillanceEvent(surveillanceEvent);
    }

    /**
     * 修改ai摄像头事件
     *
     * @param surveillanceEvent ai摄像头事件
     * @return 结果
     */
    @Override
    public int updateSurveillanceEvent(SurveillanceEvent surveillanceEvent) {
        surveillanceEvent.setUpdateTime(DateUtils.getNowDate());
        return surveillanceEventMapper.updateSurveillanceEvent(surveillanceEvent);
    }

    /**
     * 批量删除ai摄像头事件
     *
     * @param ids 需要删除的ai摄像头事件主键
     * @return 结果
     */
    @Override
    public int deleteSurveillanceEventByIds(Long[] ids) {
        return surveillanceEventMapper.deleteSurveillanceEventByIds(ids);
    }

    /**
     * 删除ai摄像头事件信息
     *
     * @param id ai摄像头事件主键
     * @return 结果
     */
    @Override
    public int deleteSurveillanceEventById(Long id) {
        return surveillanceEventMapper.deleteSurveillanceEventById(id);
    }


    @Override
    public Page<SurveillanceEvent> pageList(Page<SurveillanceEvent> page, SurveillanceEvent surveillanceEvent) {
        LambdaQueryWrapper<SurveillanceEvent> queryWrapper = getQueryWrapper(surveillanceEvent);
        queryWrapper.orderByDesc(SurveillanceEvent::getEventBeginTime);
        return page(page, queryWrapper);
    }

    @Override
    public boolean addSurveillanceEvent(SurveillanceEvent surveillanceEvent) {
        if(StringUtils.isNotBlank(surveillanceEvent.getCommunityCode())){
            commintyEventCountService.addCommunityEventCount(surveillanceEvent.getCommunityCode());
        }
        return  save(surveillanceEvent);
    }

    @Override
    public void freeSpace() {
        surveillanceEventMapper.freeSpace();
    }

    @Override
    public List<SurveillanceEvent> listAll(SurveillanceEvent surveillanceEvent) {
        return list(getQueryWrapper(surveillanceEvent));
    }

    private LambdaQueryWrapper<SurveillanceEvent> getQueryWrapper(SurveillanceEvent surveillanceEvent) {
        LambdaQueryWrapper<SurveillanceEvent> queryWrapper = Wrappers.lambdaQuery(SurveillanceEvent.class);
        queryWrapper.eq(SurveillanceEvent::getDeleted, DeletedEnum.NOMAL.getValue());

        String sql = PermissionUtil.getDeptAddressUnitSql(SecurityUtils.getDeptId());
        queryWrapper.inSql(SurveillanceEvent::getCommunityCode,sql);
//        事件编号
        if (StringUtils.isNotEmpty(surveillanceEvent.getEventCode())) {
            queryWrapper.eq(SurveillanceEvent::getEventCode, surveillanceEvent.getEventCode());
        }


//        事件持续时间
        if (Objects.nonNull(surveillanceEvent.getEventDurationTime())) {
            queryWrapper.eq(SurveillanceEvent::getEventDurationTime, surveillanceEvent.getEventDurationTime());
        }


//        事件开始时间
        if (Objects.nonNull(surveillanceEvent.getEventBeginTime())) {
            queryWrapper.eq(SurveillanceEvent::getEventBeginTime, surveillanceEvent.getEventBeginTime());
        }


//        事件结束时间
        if (Objects.nonNull(surveillanceEvent.getEventEndTime())) {
            queryWrapper.eq(SurveillanceEvent::getEventEndTime, surveillanceEvent.getEventEndTime());
        }


//        事件图片
        if (StringUtils.isNotEmpty(surveillanceEvent.getPicUrl())) {
            queryWrapper.eq(SurveillanceEvent::getPicUrl, surveillanceEvent.getPicUrl());
        }


//        本地事件图片地址
        if (StringUtils.isNotEmpty(surveillanceEvent.getLocalPicUrl())) {
            queryWrapper.eq(SurveillanceEvent::getLocalPicUrl, surveillanceEvent.getLocalPicUrl());
        }


//        图片宽度
        if (Objects.nonNull(surveillanceEvent.getImgWidth())) {
            queryWrapper.eq(SurveillanceEvent::getImgWidth, surveillanceEvent.getImgWidth());
        }


//        图片宽度
        if (Objects.nonNull(surveillanceEvent.getImageHeight())) {
            queryWrapper.eq(SurveillanceEvent::getImageHeight, surveillanceEvent.getImageHeight());
        }


//        事件类型
        if (StringUtils.isNotEmpty(surveillanceEvent.getEventType())) {
            queryWrapper.eq(SurveillanceEvent::getEventType, surveillanceEvent.getEventType());
        }


//        图片作品
        if (StringUtils.isNotEmpty(surveillanceEvent.getImgLocation())) {
            queryWrapper.eq(SurveillanceEvent::getImgLocation, surveillanceEvent.getImgLocation());
        }

        if (Objects.nonNull(surveillanceEvent.getEventStartTime())) {
            queryWrapper.ge(SurveillanceEvent::getEventBeginTime, surveillanceEvent.getEventStartTime());
        }
        if (Objects.nonNull(surveillanceEvent.getEventStopTime())) {
            queryWrapper.le(SurveillanceEvent::getEventBeginTime, surveillanceEvent.getEventStopTime());
        }
        if (Objects.nonNull(surveillanceEvent.getSurveillanceId())) {
            queryWrapper.eq(SurveillanceEvent::getSurveillanceId, surveillanceEvent.getSurveillanceId());
        }

        //        设备名称
        if (StringUtils.isNotEmpty(surveillanceEvent.getDeviceName())) {
            queryWrapper.like(SurveillanceEvent::getDeviceName, surveillanceEvent.getDeviceName());
        }


//        设备序列号
        if (StringUtils.isNotEmpty(surveillanceEvent.getSerialNumber())) {
            queryWrapper.eq(SurveillanceEvent::getSerialNumber, surveillanceEvent.getSerialNumber());
        }


//        设备编号
        if (StringUtils.isNotEmpty(surveillanceEvent.getDeviceNumber())) {
            queryWrapper.eq(SurveillanceEvent::getDeviceNumber, surveillanceEvent.getDeviceNumber());
        }


//        省份名称
        if (StringUtils.isNotEmpty(surveillanceEvent.getProviceName())) {
            queryWrapper.like(SurveillanceEvent::getProviceName, surveillanceEvent.getProviceName());
        }


//        省份编码
        if (StringUtils.isNotEmpty(surveillanceEvent.getProviceCode())) {
            queryWrapper.eq(SurveillanceEvent::getProviceCode, surveillanceEvent.getProviceCode());
        }


//        市名称
        if (StringUtils.isNotEmpty(surveillanceEvent.getCityName())) {
            queryWrapper.like(SurveillanceEvent::getCityName, surveillanceEvent.getCityName());
        }


//        市编号
        if (StringUtils.isNotEmpty(surveillanceEvent.getCityCode())) {
            queryWrapper.eq(SurveillanceEvent::getCityCode, surveillanceEvent.getCityCode());
        }


//        区名称
        if (StringUtils.isNotEmpty(surveillanceEvent.getAreaName())) {
            queryWrapper.like(SurveillanceEvent::getAreaName, surveillanceEvent.getAreaName());
        }


//        区编码
        if (StringUtils.isNotEmpty(surveillanceEvent.getAreaCode())) {
            queryWrapper.eq(SurveillanceEvent::getAreaCode, surveillanceEvent.getAreaCode());
        }


//        地址
        if (StringUtils.isNotEmpty(surveillanceEvent.getAddress())) {
            queryWrapper.eq(SurveillanceEvent::getAddress, surveillanceEvent.getAddress());
        }


//        社区
        if (StringUtils.isNotEmpty(surveillanceEvent.getCommunity())) {
            queryWrapper.eq(SurveillanceEvent::getCommunity, surveillanceEvent.getCommunity());
        }


//        小区名称
        if (StringUtils.isNotEmpty(surveillanceEvent.getResidentialName())) {
            queryWrapper.like(SurveillanceEvent::getResidentialName, surveillanceEvent.getResidentialName());
        }


//        设备品牌
        if (StringUtils.isNotEmpty(surveillanceEvent.getDeviceBrand())) {
            queryWrapper.eq(SurveillanceEvent::getDeviceBrand, surveillanceEvent.getDeviceBrand());
        }


//        设备型号
        if (StringUtils.isNotEmpty(surveillanceEvent.getDeviceModel())) {
            queryWrapper.eq(SurveillanceEvent::getDeviceModel, surveillanceEvent.getDeviceModel());
        }


//        街道名称
        if (StringUtils.isNotEmpty(surveillanceEvent.getStreetName())) {
            queryWrapper.like(SurveillanceEvent::getStreetName, surveillanceEvent.getStreetName());
        }


//        街道id
        if (StringUtils.isNotEmpty(surveillanceEvent.getStreetCode())) {
            queryWrapper.eq(SurveillanceEvent::getStreetCode, surveillanceEvent.getStreetCode());
        }


//        社区ID
        if (StringUtils.isNotEmpty(surveillanceEvent.getCommunityCode())) {
            queryWrapper.eq(SurveillanceEvent::getCommunityCode, surveillanceEvent.getCommunityCode());
        }


//        小区ID
        if (StringUtils.isNotEmpty(surveillanceEvent.getResidentialCode())) {
            queryWrapper.eq(SurveillanceEvent::getResidentialCode, surveillanceEvent.getResidentialCode());
        }

        if(StringUtils.isNotBlank(surveillanceEvent.getEventTypeStr())){
            queryWrapper.like(SurveillanceEvent::getEventTypeStr,surveillanceEvent.getEventTypeStr());
        }
        return queryWrapper;
    }
}
